#include "user.h"
#include "../TinyXML/tinyxml.h"
#include "../ChineseEnglishMapping.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QSplitter>

user::user()
{

}

void user::InitUI(const QString &fileName, QList<QPair<QWidget *, QString> > &widgets, QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    QList<account> _accountList;
    Analysis(filePath, _accountList);

    //添加第一个表
    QWidget* widget = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(widget);
    QSplitter *splitter = new QSplitter(Qt::Vertical, widget);
    splitter->setChildrenCollapsible(false);
    layout->addWidget(splitter);
    widgets.append(qMakePair(widget,QStringLiteral("tab0")));

    TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget);
    splitter->addWidget(_TableWidgetForm);
    _TableWidgetForm->setGroupBoxTitle(QString("%1_0").arg(fileName.left(fileName.size()-4)));

    //获取表头
    account _accounttemp;
    _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_accounttemp.toFieldNames().split(',')));
    _TableWidgetForm->setTableWidgetHeaderType(_accounttemp.toFieldTypes());

    for(int j = 0;j<_accountList.size();j++)
    {
        account _account = _accountList.at(j);
        int rowCount = _TableWidgetForm->getTableWidget()->rowCount();
        _TableWidgetForm->getTableWidget()->setRowCount(rowCount+1);
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 0, new QTableWidgetItem(QString("%1").arg(_account.account)));
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 1, new QTableWidgetItem(QString("%1").arg(_account.password)));
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 2, new QTableWidgetItem(QString("%1").arg(_account.type)));
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 3, new QTableWidgetItem(QString("%1").arg(_account.permission)));
    }
    m_pTableWidgetFormMap.insert(0, QList<TableWidgetForm *>() << _TableWidgetForm);
}

bool user::SaveUI(const QString &fileName, const QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    if(m_pTableWidgetFormMap.count() == 1)
    {
        QList<account> _accountList;

        TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(0).at(0);
        if(_TableWidgetForm)
        {
            for (int k = 0;k<_TableWidgetForm->getTableWidget()->rowCount();k++)
            {
                account _account;
                _account.account = _TableWidgetForm->getTableWidget()->item(k,0)->text();
                _account.password = _TableWidgetForm->getTableWidget()->item(k,1)->text();
                _account.type = _TableWidgetForm->getTableWidget()->item(k,2)->text();
                _account.permission = _TableWidgetForm->getTableWidget()->item(k,3)->text().toInt();
                _accountList.append(_account);
            }
        }

        SaveXml(filePath, _accountList);
        return true;
    }
    return false;
}

void user::Analysis(const QString &path, QList<account> &_accountList)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    TiXmlElement* rootElement = xmlDocument->FirstChildElement("user");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    if(rootElement)
    {
        TiXmlElement* useraccountElement = rootElement->FirstChildElement("useraccount");
        if(useraccountElement != nullptr)
        {
            for (TiXmlElement* accountElement = useraccountElement->FirstChildElement("account");
                 accountElement != nullptr; accountElement = accountElement->NextSiblingElement("account")) {
                if(accountElement != nullptr)
                {
                    account _account;
                    _account.account = accountElement->Attribute("account");
                    _account.password = accountElement->Attribute("password");
                    _account.type = accountElement->Attribute("type");
                    accountElement->QueryIntAttribute("permission", &_account.permission);
                    _accountList.append(_account);
                }
            }
        }
    }
    delete xmlDocument;
}

bool user::SaveXml(const QString &path, const QList<account> &_accountList)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return false;
    }

    TiXmlElement* rootElement = xmlDocument->FirstChildElement("user");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return false;
    }

    if(rootElement)
    {
        TiXmlElement* useraccountElement = rootElement->FirstChildElement("useraccount");
        if(useraccountElement != nullptr)
        {
            for (TiXmlElement* accountElement = useraccountElement->FirstChildElement("account");
                 accountElement != nullptr; accountElement = accountElement->NextSiblingElement("account")) {
                if(accountElement != nullptr)
                {
                    useraccountElement->RemoveChild(accountElement);
                }
            }
            for (auto _account : _accountList)
            {
                //元素类
                TiXmlElement* ChildxmlElement = new TiXmlElement("account");
                ChildxmlElement->SetAttribute("account", _account.account.toStdString().c_str());
                ChildxmlElement->SetAttribute("password", _account.password.toStdString().c_str());
                ChildxmlElement->SetAttribute("type", _account.type.toStdString().c_str());
                ChildxmlElement->SetAttribute("permission", _account.permission);
                useraccountElement->LinkEndChild(ChildxmlElement);
            }
        }
    }
    //保存
    xmlDocument->SaveFile(path.toStdString().c_str());

    delete xmlDocument;
    return true;
}
